package com.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.awt.Point;
import java.io.IOException;
/**
 * ClassName: PointDeserializer
 * Description:
 *      自定义一个反序列化器,  将JSON中的点坐标字符串转换为Point对象
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/05/20 20:04
 */

public class PointDeserializer extends JsonDeserializer<Point> {
    @Override
    public Point deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String pointStr = jsonParser.getText();
        // 假设字符串格式为 "x,y"，例如 "116.30651,39.97708101"
        String[] parts = pointStr.split(",");
        if (parts.length != 2) {
            throw new JsonProcessingException("Invalid point format: " + pointStr) {};
        }
        double x = Double.parseDouble(parts[0]);
        double y = Double.parseDouble(parts[1]);
        return new Point((int) x, (int) y);
    }
}